package nc.bs.hrpbm.budgetmake.util;

import java.util.Objects;
import nc.vo.hrpbm.budgetindicator.AggBudgetIndicatorVO;
import nc.vo.hrpbm.budgetindicator.BudgetIndicatorBVO;
import nc.vo.hrpbm.budgetmake.AggBudgetMakeVO;
import nc.vo.hrpbm.budgetmake.BudgetMakeDetailVO;
import nc.vo.hrpbm.hrpbmpubenum.IndicatorDimEnum;
import nc.vo.pub.BusinessException;
import nc.vo.pub.lang.UFDouble;
import nc.vo.pubapp.pattern.model.entity.bill.AbstractBill;
import nccloud.hrpbm.budget.util.SetChildColumnValue;
import org.apache.commons.collections4.map.HashedMap;

public class PutIndicatoramountToMakeDetail {
  public static void putIndicatoramountToMakeDetail(AggBudgetMakeVO[] vos, AggBudgetIndicatorVO aggBudgetIndicatorVO) throws BusinessException {
    SetChildColumnValue.setChildColumnValue(vos, true);
    if (Objects.nonNull(aggBudgetIndicatorVO) && (aggBudgetIndicatorVO.getAllChildrenVO()).length > 0) {
      BudgetIndicatorBVO[] bvos = (BudgetIndicatorBVO[])aggBudgetIndicatorVO.getAllChildrenVO();
      if (aggBudgetIndicatorVO.getParentVO().getIndicatordim().equals(IndicatorDimEnum.dept.value())) {
    	  vos[0].getParentVO().setIndicatoramount(bvos[0].getAmount()); 
    	  vos[0].getParentVO().setDef19(bvos[0].getDef19()); 
    	  vos[0].getParentVO().setDef20(bvos[0].getDef20()); 
      }
        
      if (aggBudgetIndicatorVO.getParentVO().getIndicatordim().equals(IndicatorDimEnum.schema.value())) {
        BudgetMakeDetailVO[] detailVOs = (BudgetMakeDetailVO[])vos[0].getAllChildrenVO();
        HashedMap<String, Object[]> hashedMap = new HashedMap();
        byte b;
        int i;
        BudgetIndicatorBVO[] arrayOfBudgetIndicatorBVO;
        
        for (i = (arrayOfBudgetIndicatorBVO = bvos).length, b = 0; b < i; ) {
          BudgetIndicatorBVO bvo = arrayOfBudgetIndicatorBVO[b];
            Object[] detailValue= new Object[3];
            String key = (Objects.nonNull(bvo.getPk_budgetdept()) ? bvo.getPk_budgetdept() : "null") +
                    (Objects.nonNull(bvo.getPk_fundsource()) ? bvo.getPk_fundsource() : "null") +
                    (Objects.nonNull(bvo.getPk_project()) ? bvo.getPk_project() : "null") +
                    (Objects.nonNull(bvo.getPk_inoutclass()) ? bvo.getPk_inoutclass() : "null") +
                    (Objects.nonNull(bvo.getPk_operation()) ? bvo.getPk_operation() : "null");
          detailValue[0]=bvo.getAmount();
          detailValue[1]=bvo.getDef19();
          detailValue[2]=bvo.getDef20();
          hashedMap.put(key, detailValue);
          b++;
        } 
        BudgetMakeDetailVO[] arrayOfBudgetMakeDetailVO1;
        for (i = (arrayOfBudgetMakeDetailVO1 = detailVOs).length, b = 0; b < i; ) {
          BudgetMakeDetailVO detailVO = arrayOfBudgetMakeDetailVO1[b];
          StringBuffer key = new StringBuffer();
          key.append(Objects.nonNull(detailVO.getPk_budgetdept()) ? detailVO.getPk_budgetdept() : "null");
          key.append(Objects.nonNull(detailVO.getPk_fundsource()) ? detailVO.getPk_fundsource() : "null");
          key.append(Objects.nonNull(detailVO.getPk_project()) ? detailVO.getPk_project() : "null");
          key.append(Objects.nonNull(detailVO.getPk_inoutclass()) ? detailVO.getPk_inoutclass() : "null");
          key.append(Objects.nonNull(detailVO.getPk_operation()) ? detailVO.getPk_operation() : "null");
          if(null!=hashedMap.get(key.toString())) {
        	  detailVO.setIndicatoramount(Objects.nonNull(hashedMap.get(key.toString())[0])?(UFDouble)hashedMap.get(key.toString())[0]:null);
              detailVO.setDef19(Objects.nonNull(hashedMap.get(key.toString())[1])?(String)hashedMap.get(key.toString())[1]:null);
              detailVO.setDef20(Objects.nonNull(hashedMap.get(key.toString())[2])?(String)hashedMap.get(key.toString())[2]:null);  
          }
          b++;
        } 
      } 
    } 
  }
}
